package com.hzhr.iot.device.convertor.custom;

import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.convertor.CollectDataConvertor;
import com.hzhr.iot.device.entity.CollectDataEntity;
import com.hzhr.iot.device.entity.ext.CollectDataEntityExt;
import com.hzhr.iot.device.entity.ext.CollectDeviceEntityExt;
import com.hzhr.iot.device.entity.ext.OptionsEntityExt;
import com.hzhr.iot.device.model.collectData.CollectDataModel;
import com.hzhr.iot.device.model.collectData.CollectDataPostModel;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class CollectDataCustomConvertor extends CollectDataConvertor {
    @Override
    public CollectDataEntity collectDataPostModel2entity(CollectDataPostModel in) {
        CollectDataEntity result =  super.collectDataPostModel2entity(in);
        Map<String, Object> extProp = new HashMap<>();
        if (in.getEcl() != null) {
            extProp.put("ecl", in.getEcl());
        }
        if (in.getRsrq() != null) {
            extProp.put("rsrq", in.getRsrq());
        }
        if (in.getRssi() != null) {
            extProp.put("rssi", in.getRssi());
        }
        if (in.getSnr() != null) {
            extProp.put("snr", in.getSnr());
        }
        if (in.getPci() != null) {
            extProp.put("pci", in.getPci());
        }
        if (in.getEarfcn() != null) {
            extProp.put("earfcn", in.getEarfcn());
        }
        if (extProp.size() > 0) result.setExtProp(HzhrJsonUtil.object2json(extProp));
        return result;
    }

    @Override
    public CollectDataModel ext2collectDataModel(CollectDataEntityExt in) {
        CollectDataModel result = super.ext2collectDataModel(in);
        Map extProp = HzhrJsonUtil.json2object(in.getExtProp(), Map.class);
        if (extProp != null) {
            result.setEcl(HzhrTypeUtil.object2string(extProp.get("ecl")));
            result.setRsrq(HzhrTypeUtil.object2string(extProp.get("rsrq")));
            result.setRssi(HzhrTypeUtil.object2string(extProp.get("rssi")));
            result.setSnr(HzhrTypeUtil.object2string(extProp.get("snr")));
            result.setPci(HzhrTypeUtil.object2string(extProp.get("pci")));
            result.setEarfcn(HzhrTypeUtil.object2string(extProp.get("earfcn")));
        }
        OptionsEntityExt ext1 = in.getValveStatusExt();
        if (ext1 != null) {
            result.setValveStatusDes(ext1.getDes());
        }
        CollectDeviceEntityExt ext2 = in.getCollectDeviceExt();
        if (ext2 != null) {
            result.setCollectDeviceCode(ext2.getCode());
        }
        return result;
    }

    @Override
    public CollectDataModel entity2collectDataModel(CollectDataEntity in) {
        CollectDataModel result = super.entity2collectDataModel(in);
        Map extProp = HzhrJsonUtil.json2object(in.getExtProp(), Map.class);
        result.setEcl(HzhrTypeUtil.object2string(extProp.get("ecl")));
        result.setRsrq(HzhrTypeUtil.object2string(extProp.get("rsrq")));
        result.setRssi(HzhrTypeUtil.object2string(extProp.get("rssi")));
        result.setSnr(HzhrTypeUtil.object2string(extProp.get("snr")));
        result.setPci(HzhrTypeUtil.object2string(extProp.get("pci")));
        result.setEarfcn(HzhrTypeUtil.object2string(extProp.get("earfcn")));
        return result;
    }
}

